// pages/index/order/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price:0,
      freight:0,
      zongprice:0,
      id:0,
      isShowJieSuan:0,
      isweixin:0,
      isyue:0,
      uid:0,
      phone:'',
      address:'',
      liuyan:'',
      oid:0,
      yijingxiadan:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
        price:options.price,
        freight:options.freight,
        id:options.id,
        isweixin:1,
        zongprice:parseFloat(options.price)+parseFloat(options.freight),
        uid:options.uid,
        phone:getApp().globalData.userInfo.contract,
        address:getApp().globalData.userInfo.address
    })
      if(undefined != options.oid){
        this.setData({
            oid:options.oid
        })
      }
  },
    dataChange:function(e){
      var name = e.currentTarget.dataset.name;
      var that = this;
      switch (name){
          case 'phone':
              that.setData({
                  phone:e.detail.value
              })
              break;
          case 'address':
              that.setData({
                  address:e.detail.value
              })
              break;
          case 'liuyan':
              that.setData({
                  liuyan:e.detail.value
              })
              break;
      }
    },

//显示结算
  showJieSuan:function(e){
    var that = this;
      if('' == this.data.phone || this.data.phone.length != 11){
          wx.showModal({
              title:'提示',
              content:'请正确填写手机号'
          })
          return ;
      }
      if('' == this.data.address){
          wx.showModal({
              title:'提示',
              content:'请正确填写地址'
          })
          return ;
      }

      if(0 != this.data.uid && getApp().globalData.userInfo.uid == this.data.uid){
          wx.showModal({
              title:'提示',
              content:'无法购买自己的商品'
          })
          return ;
      }

      this.setData({
      isShowJieSuan:that.data.isShowJieSuan==1?0:1
    })
  },

  //切换支付方式
  changeZhiFuType:function(e){
    var type = e.currentTarget.dataset.type;
    if(type == 'isweixin'){
      this.setData({
        isweixin:1,
        isyue:0
      })
    }else if(type == 'isyue'){
      this.setData({
        isweixin:0,
        isyue:1
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

    querengoumai:function(e){

       var that = this;
        wx.showModal({
            title:'提示',
            content:'确认购买吗？',
            success:function(res){
                if(res.confirm){
                    that.gouMaiUnuse();
                }else if(res.cancel){
                    getApp().getUserInfo();
                }
            }
        })
    },

    //确认购买
    gouMaiUnuse:function(e){
        wx.showLoading();
        var that = this;

        if(1 == that.data.isyue){
            var yuenum = getApp().globalData.userInfo.yu_e_num;
            if(parseFloat(yuenum)<parseFloat(that.data.zongprice)){
                wx.showToast({
                    title:'余额不足',
                    icon: 'success',
                    duration:3000
                })
                return;
            }
        }
        that.setData({
            yijingxiadan:1
        })
        // console.log(getApp().globalData.userInfo.openid)
        wx.request({
            url:getApp().data.server + '/index.php/Tongcheng/Book/gouMaiUnuse',
            data:{
                price:that.data.price,
                freight:that.data.freight,
                zongprice:that.data.zongprice,
                otherid:that.data.id,
                openid:getApp().globalData.userInfo.openid,
                uid:getApp().globalData.userInfo.uid,
                isyue:that.data.isyue,
                isweixin:that.data.isweixin,
                phone:that.data.phone,
                address:that.data.address,
                liuyan:that.data.liuyan,
                oid:that.data.oid
            },
            success:function(d){

                if(1 == that.data.isyue){
                    wx.showToast({
                        title:'购买成功',
                        icon: 'success',
                        duration:3000
                    })
                    setTimeout(function(){
                        wx.redirectTo({
                          url: '/pages/order/index?from=0'
                        })
                    },2000)
                    getApp().getUserInfo();
                    return;
                }
                console.log(d.data);
                // return;
                wx.requestPayment(
                    {
                        'timeStamp': d.data.timeStamp,
                        'nonceStr': d.data.nonceStr,
                        'package': d.data.package,
                        'signType': 'MD5',
                        'paySign': d.data.paySign,
                        'success':function(res){
                            console.log(res)
                            wx.showToast({
                                title:'购买成功',
                                icon: 'success',
                                duration:3000,
                                success:function(e){
                                    // that.showJieSuan();
                                }
                            })
                        },
                        'fail':function(res){
                            console.log(res)
                            if('requestPayment:fail cancel'==res.errMsg){
                                wx.showToast({
                                    title:'用户取消支付',
                                    icon: 'none',
                                    duration:3000,
                                    success:function(e){
                                        that.showJieSuan();
                                    }
                                })
                            }else{
                                wx.showToast({
                                    title:'支付失败',
                                    icon: 'none',
                                    duration:3000,
                                    success:function(e){
                                        that.showJieSuan();
                                    }
                                })
                            }
                        },
                        'complete':function(res){
                            // wx.showToast({
                            //     title:'购买成功',
                            //     icon: 'success',
                            //     duration:3000,
                            //     success:function(e){
                            //         that.showJieSuan();
                            //     }
                            // })
                        }
                    })

            }
        })
    }

})